<template>
  <div>
    <Card>
      <template #before>
        <el-button type="primary" @click="isShowDialogFn">添加分类</el-button>
      </template>
      <template #after>
        <el-input v-model="title_contains" placeholder="请输入内容" class="input-with-select" @keyup.enter.native="searchTitle" @blur="reset">
          <el-button slot="append" icon="el-icon-search" @click="searchTitle" />
        </el-input>
      </template>
    </Card>
    <el-card>
      <el-table :data="categoryList" border>
        <el-table-column label="图片" prop="image" width="160px">
          <template #default="{row}">
            <img :src="row.image" alt="" class="pic">
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title">
          <template #default="{row}">
            <div v-html="row.title.replace( title_contains,`<span style=color:red>${ title_contains}</span>`)">
              {{ row.title }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="介绍" prop="desc" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button size="small" type="primary" @click="editCategory(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delCategory(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" align="middle" justify="center" style="height:60px">
        <el-pagination
          :page-sizes="[5,10,20,30]"
          :page-size="formData._limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </el-row>
    </el-card>
    <CategoryEdit ref="categoryData" :is-show-dialog.sync="isShowDialog" @addCategory="addCategoryFn" />
  </div>
</template>

<script>
import { getCategoryList, delCategory, getCategoryCount } from '@/api/category'
import CategoryEdit from './component/category-edit.vue'
export default {
  components: { CategoryEdit },
  data() {
    return {
      formData: {
        _limit: 3, // 获取多少条
        _start: 0 // 从第几条开始
      },
      title_contains: '', // 模糊搜索
      categoryList: [], // 获取分类列表
      isShowDialog: false, // 显示隐藏弹窗
      page: {
        page: 1, // 第几页
        // pagesize: 3, // 每页显示多少条
        total: 0// 总条数
      },
      currentPage: 1
    }
  },
  async created() {
    this.getCategoryList()
    this.getCategoryCount()
  },
  methods: {
    // 每页显示的条数
    async handleSizeChange(page) {
      // console.log(72, page)
      this.formData._limit = page
      this.page.total = await getCategoryCount()
      this.getCategoryList()
    },
    // 分页按钮
    async pageChange(newPage) {
      // console.log(newPage)
      // this.page.page = newPage
      this.formData._start = (newPage - 1) * this.formData._limit
      if (!this.title_contains) {
        this.getCategoryList()
        return
      }
      this.categoryList = await getCategoryList({ ...this.formData, title_contains: this.title_contains })
    },
    // 传id给弹框组件
    editCategory(id) {
      this.$refs.categoryData.editCategory(id)
      this.isShowDialog = true
    },
    // 添加按钮
    addCategoryFn() {
      this.getCategoryList()
      this.getCategoryCount()
    },
    // 点击添加分类按钮弹窗
    isShowDialogFn() {
      this.isShowDialog = true
    },
    // 根据id删除分类
    async delCategory(id) {
      try {
        await this.$confirm('你确定要删除吗')
        await delCategory(id)
        this.$message.success('删除成功')
        this.getCategoryList()
      } catch (err) {
        console.log(err)
      }
    },
    // 搜索分类
    async searchTitle() {
      const res = await getCategoryList({ title_contains: this.title_contains })
      this.page.total = res.length
      this.formData._start = 0
      this.page.page = 1
      this.categoryList = await getCategoryList({ ...this.formData, title_contains: this.title_contains })
      console.log(this.categoryList)
    },
    // 获取分类列表
    async getCategoryList() {
      this.categoryList = await getCategoryList(this.formData)
    },
    // 获取分类管理总条数
    async getCategoryCount() {
      this.page.total = await getCategoryCount()
    },
    // 搜索框为空重置列表
    reset() {
      if (!this.title_contains) {
        this.getCategoryList()
        this.getCategoryCount()
      }
    }
  }
}
</script>

<style>
.pic{
  width: 100%;
  object-fit: cover;
}
</style>
